﻿using System;
using System.IO;
using System.Windows.Forms;
using ResxCrunch.Properties;
using ResxCrunch.Utils;
using ResxCrunch.Utils.FileData;

namespace ResxCrunch
{
    /// <summary>
    /// Save form class
    /// </summary>
    public partial class FrmSave : Form
    {
        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmSave"/> class.
        /// </summary>
        public FrmSave()
        {
            InitializeComponent();
        }
        #endregion Initialization

        #region Properties
        /// <summary>
        /// Gets or sets the selected format.
        /// </summary>
        /// <value>The selected format.</value>
        public Utils.Enums.Format SelectedFormat { get; set; }

        /// <summary>
        /// Gets the resx group data.
        /// </summary>
        /// <value>The resx group data.</value>
        public ResxGroupData ResxGroupInfo
        {
            get
            {
                return new ResxGroupData(txtResxGroupDir.Text, true);
            }
        }

        /// <summary>
        /// Gets the resource group data.
        /// </summary>
        /// <value>The resource group data.</value>
        public ResourceGroupData ResourceGroupInfo
        {
            get
            {
                return new ResourceGroupData(txtResourceGroupDir.Text, true);
            }
        }

        /// <summary>
        /// Gets the CSV file data.
        /// </summary>
        /// <value>The CSV file data.</value>
        public CsvData CsvFileData
        {
            get
            {
                return new CsvData(txtCsvFileName.Text, chkCsvHeaders.Checked, comboSeparator.SelectedIndex);
            }
        }

        /// <summary>
        /// Gets the Xml file data.
        /// </summary>
        /// <value>The Xml file data.</value>
        public XmlData XmlFileData
        {
            get
            {
                return new XmlData(txtXmlFileName.Text);
            }
        }

        /// <summary>
        /// Gets the android group data.
        /// </summary>
        /// <value>The android group data.</value>
        public AndroidGroupData AndroidGroupInfo
        {
            get
            {
                return new AndroidGroupData(txtAndroidGroupDir.Text, true);
            }
        }

        /// <summary>
        /// Gets the blackberry group data.
        /// </summary>
        /// <value>The blackberry group data.</value>
        public BlackberryGroupData BlackberryGroupInfo
        {
            get
            {
                return new BlackberryGroupData(txtBlackberryGroupDir.Text, true);
            }
        }
        #endregion Properties

        #region Private Methods
        /// <summary>
        /// Enables disables items on the form according to the situation
        /// </summary>
        private void Enablings()
        {
            gbResx.Enabled = false;
            gbResourceGroup.Enabled = false;
            gbCsv.Enabled = false;
            gbXml.Enabled = false;
            gbAndroidGroup.Enabled = false;
            gbBlackberryGroup.Enabled = false;

            if (rbResxGroup.Checked)
            {
                gbResx.Enabled = true;
            }

            if (rbResourceGroup.Checked)
            {
                gbResourceGroup.Enabled = true;
            }

            if (rbCsv.Checked)
            {
                gbCsv.Enabled = true;
            }

            if (rbXml.Checked)
            {
                gbXml.Enabled = true;
            }

            if (rbAndroidGroup.Checked)
            {
                gbAndroidGroup.Enabled = true;
            }

            if (rbBlackberryGroup.Checked)
            {
                gbBlackberryGroup.Enabled = true;
            }
        }

        /// <summary>
        /// Loads the form information.
        /// </summary>
        private void LoadForm()
        {
            txtResxGroupDir.Text = Settings.Default.FrmSave_ResxGroupDir;
            txtResourceGroupDir.Text = Settings.Default.FrmSave_ResourceGroupDir;
            txtCsvFileName.Text = Settings.Default.FrmSave_CsvPath;
            comboSeparator.SelectedIndex = Settings.Default.FrmSave_CvsSeparatorIndex;
            chkCsvHeaders.Checked = Settings.Default.FrmSave_CvsHeader;
            txtXmlFileName.Text = Settings.Default.FrmSave_XmlPath;
            txtAndroidGroupDir.Text = Settings.Default.FrmSave_AndroidGroupDir;
            txtBlackberryGroupDir.Text = Settings.Default.FrmSave_BlackberryGroupDir;

            switch (Settings.Default.FrmSave_RbIndex)
            {
                case 1:
                    rbResourceGroup.Checked = true;
                    break;
                case 2:
                    rbCsv.Checked = true;
                    break;
                case 3:
                    rbXml.Checked = true;
                    break;
                case 4:
                    rbAndroidGroup.Checked = true;
                    break;
                case 5:
                    rbBlackberryGroup.Checked = true;
                    break;
                case 0:
                default:
                    rbResxGroup.Checked = true;
                    break;
            }
        }

        /// <summary>
        /// Saves the form information.
        /// </summary>
        private void SaveForm()
        {
            Settings.Default.FrmSave_ResxGroupDir = txtResxGroupDir.Text;
            Settings.Default.FrmSave_ResourceGroupDir = txtResourceGroupDir.Text;
            Settings.Default.FrmSave_CsvPath = txtCsvFileName.Text;
            Settings.Default.FrmSave_CvsSeparatorIndex = comboSeparator.SelectedIndex;
            Settings.Default.FrmSave_CvsHeader = chkCsvHeaders.Checked;
            Settings.Default.FrmSave_XmlPath = txtXmlFileName.Text;
            Settings.Default.FrmSave_AndroidGroupDir = txtAndroidGroupDir.Text;
            Settings.Default.FrmSave_BlackberryGroupDir = txtBlackberryGroupDir.Text;

            #region Index
            if (rbResxGroup.Checked)
            {
                Settings.Default.FrmSave_RbIndex = 0;
            }

            if (rbResourceGroup.Checked)
            {
                Settings.Default.FrmSave_RbIndex = 1;
            }

            if (rbCsv.Checked)
            {
                Settings.Default.FrmSave_RbIndex = 2;
            }

            if (rbXml.Checked)
            {
                Settings.Default.FrmSave_RbIndex = 3;
            }

            if (rbAndroidGroup.Checked)
            {
                Settings.Default.FrmSave_RbIndex = 4;
            }

            if (rbBlackberryGroup.Checked)
            {
                Settings.Default.FrmSave_RbIndex = 5;
            }
            #endregion Index

            Settings.Default.Save();
        }

        /// <summary>
        /// Fills the separators combo.
        /// </summary>
        private void FillSeparatorsCombo()
        {
            comboSeparator.Items.Clear();

            foreach (string sep in Enums.CvsSeparatorLabels)
            {
                comboSeparator.Items.Add(sep);
            }
        }
        #endregion Private Methods

        #region Events
        /// <summary>
        /// Handles the Load event of the FrmSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSave_Load(object sender, EventArgs e)
        {
            FillSeparatorsCombo();
            LoadForm();

            this.Enablings();
        }

        /// <summary>
        /// Handles the FormClosing event of the FrmSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void FrmSave_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.OK)
            {
                SaveForm();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool valid = true;

            errorProviderSave.SetError(txtResxGroupDir, string.Empty);
            errorProviderSave.SetError(txtResourceGroupDir, string.Empty);
            errorProviderSave.SetError(txtXmlFileName, string.Empty);
            errorProviderSave.SetError(txtCsvFileName, string.Empty);
            errorProviderSave.SetError(txtAndroidGroupDir, string.Empty);
            errorProviderSave.SetError(txtBlackberryGroupDir, string.Empty);

            if (rbResxGroup.Checked)
            {
                if (txtResxGroupDir.Text.Length == 0)
                {
                    errorProviderSave.SetError(txtResxGroupDir, "Path cannot be empty.");
                    valid = false;
                }
                else
                {
                    errorProviderSave.SetError(txtResxGroupDir, string.Empty);
                }

                this.SelectedFormat = Utils.Enums.Format.ResxGroup;
            }

            if (rbResourceGroup.Checked)
            {
                if (txtResourceGroupDir.Text.Length == 0)
                {
                    errorProviderSave.SetError(txtResourceGroupDir, "Path cannot be empty.");
                    valid = false;
                }
                else
                {
                    errorProviderSave.SetError(txtResourceGroupDir, string.Empty);
                }

                this.SelectedFormat = Utils.Enums.Format.ResourceGroup;
            }

            if (rbCsv.Checked)
            {
                if (txtCsvFileName.Text.Length == 0)
                {
                    errorProviderSave.SetError(txtCsvFileName, "Path cannot be empty.");
                    valid = false;
                }
                else
                {
                    errorProviderSave.SetError(txtCsvFileName, string.Empty);

                    FileInfo fi = new FileInfo(txtCsvFileName.Text);

                    if (fi.Exists)
                    {
                        if (MessageBox.Show(this, string.Format("File '{0}' already exists. Override?", txtCsvFileName.Text), Settings.Default.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                        {
                            txtCsvFileName.Focus();
                            return;
                        }
                    }
                }

                this.SelectedFormat = Utils.Enums.Format.Csv;
            }

            if (rbXml.Checked)
            {
                if (txtXmlFileName.Text.Length == 0)
                {
                    errorProviderSave.SetError(txtXmlFileName, "Path cannot be empty.");
                    valid = false;
                }
                else
                {
                    errorProviderSave.SetError(txtXmlFileName, string.Empty);

                    FileInfo fi = new FileInfo(txtXmlFileName.Text);

                    if (fi.Exists)
                    {
                        if (MessageBox.Show(this, string.Format("File '{0}' already exists. Override?", txtXmlFileName.Text), Settings.Default.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                        {
                            txtXmlFileName.Focus();
                            return;
                        }
                    }
                }

                this.SelectedFormat = Utils.Enums.Format.Xml;
            }

            if (rbAndroidGroup.Checked)
            {
                if (txtAndroidGroupDir.Text.Length == 0)
                {
                    errorProviderSave.SetError(txtAndroidGroupDir, "Path cannot be empty.");
                    valid = false;
                }
                else
                {
                    errorProviderSave.SetError(txtAndroidGroupDir, string.Empty);
                }

                this.SelectedFormat = Utils.Enums.Format.AndroidGroup;
            }

            if (rbBlackberryGroup.Checked)
            {
                if (txtBlackberryGroupDir.Text.Length == 0)
                {
                    errorProviderSave.SetError(txtBlackberryGroupDir, "Path cannot be empty.");
                    valid = false;
                }
                else
                {
                    errorProviderSave.SetError(txtBlackberryGroupDir, string.Empty);
                }

                this.SelectedFormat = Utils.Enums.Format.BlackberryGroup;
            }

            if (valid)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnCsvFilename control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCsvFilename_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Comma Separated Values (*.csv)|*.csv|All files (*.*)|*.*";
            sfd.FilterIndex = 0;
            sfd.OverwritePrompt = false;

            if (txtCsvFileName.Text.Length > 0)
            {
                FileInfo fi = new FileInfo(txtCsvFileName.Text);
                sfd.InitialDirectory = fi.DirectoryName;
            }

            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                txtCsvFileName.Text = sfd.FileName;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnLoadResxGroupDir control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnLoadResxGroupDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = txtResxGroupDir.Text;
            fbd.ShowDialog();

            txtResxGroupDir.Text = fbd.SelectedPath;
        }

        /// <summary>
        /// Handles the Click event of the btnXml control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnXml_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Xml files (*.xml)|*.xml|All files (*.*)|*.*";
            sfd.FilterIndex = 0;
            sfd.OverwritePrompt = false;

            if (txtXmlFileName.Text.Length > 0)
            {
                FileInfo fi = new FileInfo(txtXmlFileName.Text);
                sfd.InitialDirectory = fi.DirectoryName;
            }

            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                txtXmlFileName.Text = sfd.FileName;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnLoadResourceGroupDir control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnLoadResourceGroupDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = txtResourceGroupDir.Text;
            fbd.ShowDialog();

            txtResourceGroupDir.Text = fbd.SelectedPath;
        }

        /// <summary>
        /// Handles the Click event of the btnLoadAndroidGroupDir control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnLoadAndroidGroupDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = txtAndroidGroupDir.Text;
            fbd.ShowDialog();

            txtAndroidGroupDir.Text = fbd.SelectedPath;
        }

        /// <summary>
        /// Handles the Click event of the btnLoadBlackberryGroupDir control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnLoadBlackberryGroupDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = txtBlackberryGroupDir.Text;
            fbd.ShowDialog();

            txtBlackberryGroupDir.Text = fbd.SelectedPath;
        }

        /// <summary>
        /// Handles the CheckedChanged event of all the DatatypeRadioButton controls.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void DatatypeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.Enablings();
        }
        #endregion Events
    }
}
